package com.sgb.kjwl.view.ui.activity.self.company

import android.content.Context
import android.content.Intent
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.BR
import com.sgb.kjwl.R
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.databinding.ActivityInviteBinding
import com.sgb.kjwl.view.ui.fragment.self.company.InviteFragment
import com.sgb.kjwl.viewmodel.self.company.InviteViewModel

/**
 * 邀请
 */
class InviteActivity : AppActivity() {
    private lateinit var mBinding : ActivityInviteBinding
    private lateinit var mViewModel : InviteViewModel
    private lateinit var mAdapter : InquiryPageAdapter
    private val mTitles = arrayOf("收到的邀请", "发起的邀请")
    private val mSecondTitles = arrayOf("报名", "竞价", "加盟", "报价", "会员邀请", "会员审核")
    private var fragmentList = ArrayList<Fragment>()

    companion object{
        fun launch(context: Context){
            val intent  = Intent(context, InviteActivity::class.java)
            context.startActivity(intent)
        }
    }

    /**
     * 界面初始化
     */
    override fun initView() {
        super.initView()
        mViewModel = InviteViewModel()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_invite)
        mBinding.setVariable(BR.mmodel, mViewModel)
        mBinding.lifecycleOwner = this

        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.include.titleView.text = "待办邀请"
        renderFirstTab()
        renderSecondTab()
    }

    /**
     * 一级布局初始化
     */
    private fun renderFirstTab(){
        for (item in mTitles) {
            val tab: TabLayout.Tab = mBinding.titleTabView.newTab()
            tab.text = item
            mBinding.titleTabView.addTab(tab)
        }
        mBinding.titleTabView.getTabAt(0)!!.select()
        mBinding.titleTabView.tabMode = TabLayout.MODE_FIXED
        mBinding.titleTabView.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                refreshInquiryFragment(mBinding.titleSelectedTabView.selectedTabPosition)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
    }

    /**
     * 二级布局初始化
     */
    private fun renderSecondTab() {
        fragmentList.clear()
        for (item in mSecondTitles) {
            fragmentList.add(InviteFragment.newInstance(item))
        }
        mAdapter = InquiryPageAdapter(supportFragmentManager,fragmentList,mSecondTitles)
        mBinding.titleViewPager.adapter = mAdapter
        mBinding.titleViewPager.offscreenPageLimit = fragmentList.size
        mBinding.titleSelectedTabView.setupWithViewPager(mBinding.titleViewPager)
        mBinding.titleSelectedTabView.tabMode = TabLayout.MODE_AUTO
        mBinding.titleViewPager.currentItem = 0
        mBinding.titleViewPager.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                refreshInquiryFragment(position)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }

    fun refreshInquiryFragment(position: Int){
        var fragment = fragmentList[position]!!
        if (fragment is InviteFragment){
            val onLazyLoad = fragment
            onLazyLoad.onLazyLoad()
        }
    }

    class InquiryPageAdapter(fm : FragmentManager, fragmentList : ArrayList<Fragment>,mSecondTitles : Array<String>) : FragmentPagerAdapter(fm) {
        private var fragmentList:ArrayList<Fragment> ?= null
        private var mSecondTitles:Array<String> ?= null

        init {
            this.fragmentList = fragmentList
            this.mSecondTitles = mSecondTitles
        }

        override fun getCount(): Int {
            return if (fragmentList == null) 0 else fragmentList!!.size
        }

        override fun getItem(position: Int): Fragment {
            return fragmentList!![position]
        }

        override fun getPageTitle(position: Int): CharSequence {
            return mSecondTitles!![position]
        }
    }

}